package com.example.config.udpsocket;

import com.example.common.utils.CacheUtils;
import com.example.common.utils.SpringUtils;
import com.example.service.SuppliesService;
import com.example.service.TEquipmentService;
import lombok.Data;
import lombok.Value;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;
import java.io.IOException;
import java.math.BigInteger;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;
import java.nio.ByteBuffer;

@Slf4j
@Data
@Configuration
public class UDPClientSocket implements Runnable{

    DatagramSocket socket = null;
    DatagramPacket packet = null;
    public static UDPClientSocket udpClientSocket;
    public UDPClientSocket(DatagramSocket socket,DatagramPacket packet) {
        this.socket = socket;
        this.packet = packet;
    }
    public UDPClientSocket(){

    }

    @PostConstruct
    public void init() {
        udpClientSocket = this;
    }


//    @Autowired
//    private TEquipmentService equipmentService;



    @Override
    public void run() {
        String info = null;
        InetAddress address = null;
        int port = 2325;
        byte[] data2 = null;
        DatagramPacket packet2 = null;
        TEquipmentService equipmentService =SpringUtils.getBean(TEquipmentService.class);
        try {
            info = new String(packet.getData(), 0, packet.getLength());
//            log.info("我是服务器，客户端说："+info);
            byte[] bytes = packet.getData();
            address = packet.getAddress();
            String mac_id = hex10To16(bytes[18])
                    +":"+hex10To16(bytes[19])
                    +":"+hex10To16(bytes[20])
                    +":"+hex10To16(bytes[21])
                    +":"+hex10To16(bytes[22])
                    +":"+hex10To16(bytes[23]);
            //状态解析
            //倒序
            String status = new StringBuffer(Integer.toBinaryString(bytes[42]& 0XFF)).reverse().toString()
                    + new StringBuffer(Integer.toBinaryString(bytes[43]& 0XFF)).reverse().toString();
            log.info("设备编号："+mac_id+",告警状态："+status);
            //对照缓存，相同return
            Object statusCache = CacheUtils.get("mac_id"+mac_id);
            if(statusCache == null){
                CacheUtils.put("mac_id"+mac_id,status);
            }else {
                if(status.equals(statusCache.toString())){
                    return;
                }else {
                    CacheUtils.put("mac_id"+mac_id,status);
                }
            }
            for (int i = 0; i < status.length(); i++) {
                String ainfo =String.valueOf(status.charAt(i)) ;
                //Ainfo  0告警  1正常
                Long Ainfo = Long.valueOf(ainfo);
                int warningNum = i+1;
//              log.info("第"+warningNum+"路告警");
                //处理告警业务
                equipmentService.handleWarningInfo(mac_id,warningNum,info,Ainfo);
            }
//            port = packet.getPort();
//            data2 = "我在响应你！".getBytes();
//            packet2 = new DatagramPacket(data2, data2.length, address, port);
//            socket.send(packet2);
        } catch (Exception e) {
            e.printStackTrace();
        }
        //socket.close();不能关闭
    }
    /*
    * 10进制转16进制
    * */
    static String hex10To16(byte b){
        String value = Integer.toHexString(b& 0XFF);
        if(Integer.parseInt(value) < 10){
            value = "0"+value;
        }
        return value;
    }

}
